package beta;


import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;



public class Theme
{
    protected Font defaultFont;
    
    // USTAWIENIA ZWIĄZANE Z MENU ----------------------------------------------
    protected Font menuFont;
    protected int menuBgColor = 0x0;
    protected int menuFontColor = 0x00ffff;
    protected int menuPressedFontColor = 0xffff00;
    
    // USTAWIENIA ZWIĄZANE Z TYTUŁEM -------------------------------------------
    protected Font titleFont;
    protected int titleBgColor = 0x0;
    protected int titleFontColor = 0xffffff;
    
    // USTAWIENIA ZWIĄZANE Z EKRANEM -------------------------------------------
    protected int bgColor = 0x0;//0xffff00;
    protected int fontColor = 0xffffff;//0x0;
    protected int selectionBorderColor = 0xffffff;//0x0;
    protected int selectionBorderWidth = 2;
    protected int padding = 4;
    
    // USTAWIENIA ZWIĄZANE Z SUWAKAMI ------------------------------------------
    protected int scrollBarColor = 0xffffff;//0x0;
    protected int scrollBarThickness = 6;
    protected int scrollBarPadding = 1;
    protected int scrollBarMinBarSize = 10;
    
    // INNE USTAWIENIA ZWIĄZANE Z ELEMENTAMI FORMY -----------------------------
    protected Font labelFont;
    
    public Theme()
    {
        defaultFont = Font.getDefaultFont();
        menuFont = Font.getFont(defaultFont.getFace(), Font.STYLE_PLAIN, Font.SIZE_SMALL);
        titleFont = Font.getFont(defaultFont.getFace(), Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        labelFont = Font.getFont(defaultFont.getFace(), Font.STYLE_PLAIN, Font.SIZE_SMALL);
    }
    
    /**
     * Zwraca wysokość paska menu.
     * @return
     */
    public int getMenuBarHeight()
    {
        return menuFont.getHeight() + 3;
    }
    
    /**
     * Zwraca wysokość paska tytułowego.
     * @return
     */
    public int getTitleHeight()
    {
        return titleFont.getHeight() + 3;
    }
    
    public int getPadding()
    {
        return padding;
    }
    
    public int getScrollBarSpace()
    {
        return scrollBarThickness + scrollBarPadding;
    }

    /**
     * Odpowiada za narysowanie paska z menu.
     * @param g
     * @param title
     * @param width
     * @param height
     */
    public void drawTitle(Graphics g, String title, int width, int height)
    {
        drawTitleBackground(g, width, height);
        g.setFont(titleFont);
        g.setColor(titleFontColor);
        g.drawString(title, width / 2, 1, Graphics.HCENTER | Graphics.TOP);
    }
    
    public void drawTitleBackground(Graphics g, int width, int height)
    {
        g.setColor(titleBgColor);
        g.fillRect(0, 0, width, height);
        draw3DLineH(g, titleBgColor, false, 0, height, width, height);
    }
    
    /**
     * Odpowiada za narysowanie paska menu.
     * @param g
     * @param width
     * @param height
     */
    public void drawMenuBar(Graphics g, String left, boolean leftPressed, 
          String right, boolean rightPressed, int width, int height)
    {
        drawMenuBarBackground(g, width, height);
        g.setFont(menuFont);
        
        if(left == null) left = "";
        if(right == null) right = "";
        
        g.setColor(leftPressed ? menuPressedFontColor : menuFontColor);
        g.drawString(left, menuFont.stringWidth(" "), 2, 0);
        g.setColor(rightPressed ? menuPressedFontColor : menuFontColor);
        g.drawString(right, width - menuFont.stringWidth(" "), 2, Graphics.TOP | Graphics.RIGHT);
    }
    
    /**
     * Odpowiada za narysowanie tła paska menu.
     * @param g
     * @param width
     * @param height
     */
    public void drawMenuBarBackground(Graphics g, int width, int height)
    {
        g.setColor(menuBgColor);
        g.fillRect(0, 0, width, height);
        draw3DLineH(g, menuBgColor, true, 0, 0, width, 0);
    }
 
    
    /**
     * Odpowiada za narysowanie tła ekranu.
     * @param g
     * @param width
     * @param height
     */
    public void drawScreenBackground(Graphics g, int width, int height)
    {
        g.setColor(bgColor);
        g.fillRect(0, 0, width, height);
    }
    
    public void drawVerticalScrollBar(Graphics g, int height, int value, int maxValue, int pageSize)
    {
        g.setColor(scrollBarColor);
        g.translate(scrollBarPadding, 0);
        g.drawLine(scrollBarThickness / 2, 0, scrollBarThickness / 2, height - 1);
        int barSize = (int)((float)pageSize / maxValue * height);
        if(barSize < scrollBarMinBarSize)
            barSize = scrollBarMinBarSize;
        height -= barSize;
        int position = (int)((float)value / maxValue * height);
        g.fillRect(0, position, scrollBarThickness, barSize);
        g.translate(-scrollBarPadding, 0);
    }
    
    /**
     * Odpowiedzialna za narysowanie pustego ekranu.
     * @param g
     * @param screenWidth
     * @param screenHeight
     */
    public void drawNullScreen(Graphics g, int screenWidth, int screenHeight)
    {
        drawScreenBackground(g, screenWidth, screenHeight);
        g.setColor(fontColor);
        g.setFont(defaultFont);
        g.drawString("(Brak ekranu)", screenWidth / 2, (screenHeight - defaultFont.getHeight()) / 2, Graphics.TOP | Graphics.HCENTER);
    }
    
    public void draw3DLineH(Graphics g, int color, boolean up, int x1, int y1, int x2, int y2)
    {
        int color1 = color + 0x444444;  // kolor mniej rozjaśniony
        int color2 = color + 0x888888;  // kolor bardziej rozjaśniony
        if(up == false)
        {
            y1 -= 2;
            y2 -= 2;
        }
        g.setColor(up ? color2 : color1);
        g.drawLine(x1, y1, x2, y2);
        g.setColor(up ? color1 : color2);
        g.drawLine(x1, y1 + 1, x2, y2 + 1);
    }
    
    public void drawSelectionBorder(Graphics g, int width, int height)
    {
        g.setColor(selectionBorderColor);
        g.drawRoundRect(0, 0, width - 1, height - 1, 10, 10);
    }
}
